#!/bin/bash
# 使用方式：./generate_js_result.sh -j <目标JS文件夹路径> [--d8-path <d8可执行文件路径>] [--timeout <超时时间(秒)>]
# 核心功能：直接执行JS文件并生成对应的-result.txt结果文件

js_target_dir=""
d8_path=""
timeout_seconds=30

parse_args() {
    while [[ $# -gt 0 ]]; do
        case "$1" in
            -j|--js-dir)
                if [[ -z "$2" || "$2" =~ ^- ]]; then
                    echo "错误: -j/--js-dir 必须指定有效的目标JS文件夹路径"
                    exit 1
                fi
                js_target_dir="$2"
                shift 2
                ;;
            --d8-path)
                d8_path="$2"
                shift 2
                ;;
            --timeout)
                timeout_seconds="$2"
                if ! [[ "$timeout_seconds" =~ ^[1-9][0-9]*$ ]]; then
                    echo "错误: 超时时间必须是正整数（当前输入：$timeout_seconds）"
                    exit 1
                fi
                shift 2
                ;;
            *)
                echo "未知参数: $1"
                echo "使用帮助: ./generate_js_result.sh -j <目标JS文件夹路径> [--d8-path <d8路径>] [--timeout <秒数>]"
                exit 1
                ;;
        esac
    done

    if [[ -z "$js_target_dir" ]]; then
        echo "错误: 必须通过 -j/--js-dir 指定目标JS文件夹路径"
        echo "示例: ./generate_js_result.sh -j ./hermes-tests"
        exit 1
    fi
    if [[ ! -d "$js_target_dir" ]]; then
        echo "错误: 目标JS文件夹 '$js_target_dir' 不存在或不是有效目录"
        exit 1
    fi
    if [[ -z "$d8_path" ]]; then
        echo "错误: 必须通过 --d8-path 指定d8可执行文件路径"
        exit 1
    fi
    
    if [[ ! -x "$d8_path" ]]; then
        echo "错误: 指定的d8路径 '$d8_path' 不是可执行文件"
        exit 1
    fi
}

find_all_js_files() {
    local js_dir="$1"
    find "$js_dir" -type f -name "*.js" | sort
}

generate_single_result() {
    local js_file="$1"                 
    local js_filename=$(basename "$js_file")
    local js_dir=$(dirname "$js_file")
    local result_base="${js_dir}/${js_filename%.js}"
    local result_file="${result_base}-result.txt"
    local temp_output="${result_base}-temp.txt"

    timeout "$timeout_seconds"s "$d8_path" --expose-gc "$js_file" > "$temp_output" 2>&1
    local exit_code=$?

    if [[ $exit_code -ne 0 ]]; then
        rm -f "$temp_output"
        return 1
    fi

    mv "$temp_output" "$result_file" 2>/dev/null
    if [[ -f "$result_file" ]]; then
        return 0
    else
        return 1
    fi
}

main() {
    parse_args "$@"
    
    echo "开始生成JS执行结果..."
    local js_files=($(find_all_js_files "$js_target_dir"))
    local total_js=${#js_files[@]}
    
    if [[ $total_js -eq 0 ]]; then
        echo "错误: 在 '$js_target_dir' 中未找到任何.js文件"
        exit 1
    fi

    local success_count=0
    local fail_count=0 
    local fail_list=()

    for js_file in "${js_files[@]}"; do
        if generate_single_result "$js_file"; then
            ((success_count++))
        else
            ((fail_count++))
            fail_list+=("$js_file")
        fi
    done

    if [[ $fail_count -eq 0 ]]; then
        echo "All JS Execute Success"
    else
        echo "Failed files:"
        for failed_file in "${fail_list[@]}"; do
            echo "$failed_file"
        done
    fi

    exit 0
}

main "$@"